import React from 'react'
import "../assets/css/detali.css"
import { useLocation } from 'react-router-dom'
import { useEffect, useState } from 'react'
import { getGoodsInfo, postCartadd } from '../request/api'
import { Link, useNavigate } from 'react-router-dom'
import { Toast } from "antd-mobile";
function Detail() {
    const { state } = useLocation()
    let nav = useNavigate()
    console.log(state)
    const [data, setdata] = useState({})
    const getData = () => {
        getGoodsInfo({ id: state.id }).then(res => {
            if (res.data.code == 200) {
                console.log(res, 123);
                setdata(res.data.list[0])
            }
        })
    }

    function cateAdd() {
        let userinfo = sessionStorage.getItem("userinfo")?JSON.parse(sessionStorage.getItem("userinfo")):{}
        // console.log(uid, "123")
        if (userinfo.uid){
            postCartadd({
                uid:userinfo.uid,
                goodsid: state.id,
                num: 1,
                type:state.islimit
            }).then(res => {
                // console.log(res);
                if (res.data.code == 200) {
                    console.log(res)
                    nav('/index/cart')
                }
            })
            console.log()
        } else{
            Toast.show({
                content: "赶紧去登录",
            });
            nav('/login')
            return
        }
    }

    useEffect(() => {
        getData()
    }, [])
    // console.log(setTopics)
    return (<div>
        <div className='cateTo1'>
            <div>＜</div>
            <span className='catename2'>商品详情</span>
        </div>
        <div className='nva'>
            <a href="">商品</a>
            <a href="">评价</a>
            <a href="">详情</a>
            <a href="">推荐</a>
        </div>
        {
            <div key={data.id}>
                <div><img className='imgA' src={data.img} alt="" /></div>
                <div className='detail_bd'>
                    <p><span className='span1'>限时</span><i className='i'>￥{data.price}</i><i className=''>￥{data.market_price}</i></p>
                    <p className='pp'>{data.goodsname}</p>
                    <p className='p1'>{data.description}</p>

                </div>
            </div>
        }
        <div className='detail_imgg'>
            <div className='detail_12'>
                <div className='detail_name1'>
                    <img src={require('../assets/img/iconshortcutcommodifyhotline@3x.png')} alt="" />
                    <img src={require('../assets/img/1iconshortcutcommodifyhotline@3x.png')} alt="" />
                    <img src={require("../assets/img/iconshortcutcommodifyhotlin1e@3x.png")} alt="" />
                </div>
                <div className='detail_name'>
                    <span>客服</span>
                    <span>店铺</span>
                    <span>购物车</span>
                </div>
            </div>
            <div className='detail_shouc'>

                <button onClick={() => cateAdd()}>加入购物车</button>

                <Link to={'/index/Discount'} state={{ id: data.id }}>
                    <button className='button1'>立即购买</button>
                </Link>
            </div>
        </div>
    </div>)
}
export default Detail